package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.entity.RiskTestEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * Created by sxy on 2017/11/2.
 */

public class RiskTestListAdapter extends BaseAdapter {
    private List<RiskTestEntity> mList;
    private HashMap<Integer,Integer> radioChecked;
    private Context mContext;
    private JSONObject answers;

    public JSONObject getAnswers() {
        return answers;
    }

    public RiskTestListAdapter(Context context) {
        mContext = context;
        answers=new JSONObject();
        radioChecked=new HashMap<>();
    }

    public void setDataList(List<RiskTestEntity> list){
        mList=list;
    }
    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public RiskTestEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if(convertView == null){
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_risk_test_list, null);
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.rg_content = (RadioGroup) convertView.findViewById(R.id.rg_content);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        RiskTestEntity entity=mList.get(position);
        holder.tv_number.setText(entity.getNumber()+"/"+mList.size());
        holder.tv_title.setText(entity.getTitle());
        holder.rg_content.removeAllViews();
        List<RiskTestEntity.Option> options=entity.getOption();
        if (options!=null&&options.size()>0){
            for (int i=0;i<options.size();i++){
                final RadioButton radioButton=new RadioButton(mContext);
                radioButton.setText(options.get(i).getContent());
                radioButton.setTag(options.get(i).getIndex());
                radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            answers.put((position+1)+"",radioButton.getTag().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                holder.rg_content.addView(radioButton);
            }
        }
        if (!answers.isNull((position+1)+"")){
            try {
                String index=answers.getString((position+1)+"");
                switch (index){
                    case "a":
                        ((RadioButton)holder.rg_content.getChildAt(0)).setChecked(true);
                        break;
                    case "b":
                        ((RadioButton)holder.rg_content.getChildAt(1)).setChecked(true);
                        break;
                    case "c":
                        ((RadioButton)holder.rg_content.getChildAt(2)).setChecked(true);
                        break;
                    case "d":
                        ((RadioButton)holder.rg_content.getChildAt(3)).setChecked(true);
                        break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return convertView;
    }

    class  Holder{
        private TextView tv_number;
        private TextView tv_title;
        private RadioGroup rg_content;
    }
}
